package com.icehand.utils.encryption;

import com.icehand.utils.byte2.ByteUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.MessageDigest;

/**
 * MD5工具类
 */
public class MD5Utils {
    private static final String MD5 = "MD5";
    private static Logger logger = LoggerFactory.getLogger(MD5Utils.class);

    public static String encode(final String origin, String charset) {
        String result = null;
        try {
            result = origin;
            MessageDigest md = MessageDigest.getInstance(MD5);
            if (charset == null || "".equals(charset)){
                result = ByteUtils.bytes2HexString(md.digest(result
                        .getBytes()));

            }else{
                result = ByteUtils.bytes2HexString(md.digest(result
                        .getBytes(charset)));
            }
        } catch (Exception e) {
            logger.info("Md5 error", e);
        }
        return result;
    }
}
